package com.alibaba.citrus.ots.sales.plan.repository;

import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.sales.plan.sdo.CheckPasswordRuleSDO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.bcots.model.dto.BoCheckPasswordCreateRequest;
import com.epoch.app.bcots.model.dto.BoCheckPasswordGetListRequest;
import com.epoch.app.bcots.model.dto.BoCheckPasswordGetListResponse;
import com.epoch.app.bcots.service.BcOtsService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class CheckPasswordRuleRepositoryImpl implements CheckPasswordRuleRepository {

    @Resource
    private BcOtsService bcOtsService;

    @Override
    @RepositoryInvoker(printLog = true)
    public Object createCheckPasswordRule(CheckPasswordRuleSDO checkPasswordRuleSDO) {

        BoCheckPasswordCreateRequest boCheckPasswordCreateRequest = new BoCheckPasswordCreateRequest();
        boCheckPasswordCreateRequest.setCount(checkPasswordRuleSDO.getCount());
        boCheckPasswordCreateRequest.setValidity(checkPasswordRuleSDO.getValidity());
        boCheckPasswordCreateRequest.setLockTime(checkPasswordRuleSDO.getLockTime());

        Object object = bcOtsService.boCheckPasswordCreate(boCheckPasswordCreateRequest);
        return object;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public CheckPasswordRuleSDO queryCheckPasswordRule() {

        BoCheckPasswordGetListRequest boCheckPasswordGetListRequest = new BoCheckPasswordGetListRequest();
        BoCheckPasswordGetListResponse boCheckPasswordGetListResponse = bcOtsService.boCheckPasswordGetList(boCheckPasswordGetListRequest);
        List result = boCheckPasswordGetListResponse.getResult();
        CheckPasswordRuleSDO checkPasswordRuleSDO = new CheckPasswordRuleSDO();
        if (CollectionUtils.isNotEmpty(result)) {
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(result.get(0)));
            checkPasswordRuleSDO.setCount(jsonObject.getInteger("count"));
            checkPasswordRuleSDO.setValidity(jsonObject.getLong("validity"));
            checkPasswordRuleSDO.setLockTime(jsonObject.getLong("lockTime"));
        }
        return checkPasswordRuleSDO;
    }
}
